package com.nextgenapp.gwaveapp.appengine;

import static com.google.wave.api.EventType.BLIP_SUBMITTED;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Blip;
import com.google.wave.api.Event;
import com.google.wave.api.Gadget;
import com.google.wave.api.GadgetView;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;
import com.google.wave.api.impl.GadgetViewImpl;

@SuppressWarnings("serial")
public class GwaveappServlet extends AbstractRobotServlet {
	
	private static String GADGET_URL = "http://wave-as-client.googlecode.com/svn/trunk/unimportant/wave_gadget_robot_communication_trial/web/wave_gadget_robot_communication_trial.xml";
	
	private static final Logger log = Logger.getLogger(GwaveappServlet.class.getName());
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
	}
	
	@Override
	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();
		
		log.log(Level.FINEST, "processEvent received. bundle = " + bundle.toString());
		
		if (bundle.wasSelfAdded()) {
			  Blip blip = wavelet.appendBlip();
			  TextView textView = blip.getDocument();
			  textView.append("Robot tester inserting Gadget Test!");
			  //add gadget
			  wavelet.getRootBlip().getDocument().insertElement(0, new Gadget(GADGET_URL));
		  }
		  
		  //we will process blip submitted in here
		  for (Event event : bundle.getEvents() ){
			  //Get the gadget
		      Blip blip = event.getBlip();
		      TextView textView = blip.getDocument();
		      GadgetView gadgetView = textView.getGadgetView();
		      Gadget gadget = gadgetView.getGadget(GADGET_URL);
			  switch ( event.getType()){
			  case BLIP_SUBMITTED:
			        if ( gadget != null ){
			    	  	textView.append("4.0 BLIP_SUBMITTED initiated by " + event.getModifiedBy() + "\n");
			        	//retrieve the testkey
			        	String key = gadget.getField(Const.TEST_KEY_GADGET);
			        	textView.append("Found Gadget key = " + key + "\n");
			        	key = gadget.getField(Const.TEST_KEY_ROBOT);
			        	textView.append("Found Robot key = " + key + "\n");
			        	textView.append("Setting " + Const.TEST_KEY_GADGET + " = Testing123 \n");
			        	gadget.setField(Const.TEST_KEY_GADGET, "Testing123");
			        	textView.append("Setting " + Const.TEST_KEY_ROBOT + " = Set By Robot " + BLIP_SUBMITTED + new Date() + "\n");
			        	gadget.setField(Const.TEST_KEY_ROBOT, "Set By Robot" + BLIP_SUBMITTED + new Date());
			        	GadgetViewImpl gadgets = (GadgetViewImpl)event.getBlip().getDocument().getGadgetView();
			        	gadgets.updateGadgetState(gadget);
			        	
			        	gadget.setField(Const.TEST_KEY2, "set test key 2 from robot " + BLIP_SUBMITTED + new Date());
			        	textView.append("Setting " + Const.TEST_KEY2 + " = Set By Robot " + BLIP_SUBMITTED + new Date() + "\n");
			        	gadgets = (GadgetViewImpl)event.getBlip().getDocument().getGadgetView();
			        	gadgets.updateGadgetState(gadget);

			        }
				  break;
			  case BLIP_DELETED:
		    	  textView.append("BLIP_DELETED initiated...\n");
				  blip = wavelet.appendBlip();
				  textView = blip.getDocument();
				  textView.append("Blip deleted event received!\n");
				  break;
			  case WAVELET_BLIP_CREATED:
		    	  textView.append("WAVELET_BLIP_CREATED initiated...\n");
				  blip = wavelet.appendBlip();
				  textView = blip.getDocument();
				  textView.append("Wavelet blip created event received!\n");
				  break;
			  //case DOCUMENT_CHANGED:
				  //too many events received! Comment out for now...
		    	  //textView.append("DOCUMENT_CHANGED initiated...\n");
				  //textView.append("Document Changed event received!\n");
				  //break;
			  case WAVELET_BLIP_REMOVED:
		    	  textView.append("WAVELET_BLIP_REMOVED initiated...\n");
				  blip = wavelet.appendBlip();
				  textView = blip.getDocument();
				  textView.append("Wavelet blip removed event received!\n");
				  break;
			  case WAVELET_PARTICIPANTS_CHANGED:
		    	  textView.append("WAVELET_PARTICIPANTS_CHANGED initiated...\n");
		    	  //get a list of added participants
		    	  for (String p : event.getAddedParticipants() ){
		    		  textView.append("Wavelet participant added: " + p + "\n");
		    		  //send a private blip to the participant added
		    		  ArrayList<String> pStr = new ArrayList<String>();
		    		  pStr.add(p);
		    		  Wavelet wl = event.getWavelet().createWavelet(pStr, p + "_datadocument");
		    		  //print out the wavelet document
		    		  textView.append("Created private blip with data document = " + 
		    				  wl.getDataDocument(p + "_datadocument") + "\n");
		    		  Blip privateBlip = wl.appendBlip();
		    		  privateBlip.getDocument().append("Hello! " + p + " This is a private blip just for you!");
		    	  }
		    	  
		    	  //get a list of removed participants
		    	  //TODO: REMOVE PARTICIPANT NOT WORKING. Throwing exceptions...
		    	  //for (String p : event.getRemovedParticipants() ){
		    		//  textView.append("Wavelet participant removed: " + p + "\n");
		    	  //}
				  break;
			  }
			    
		  }
		  
	}

}
